package com.myexam.action;

import java.util.Map;



import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.myexam.model.Student;
import com.myexam.model.Teacher;
import com.myexam.service.StudentService;
import com.myexam.service.TeacherService;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class LoginAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = -2517567067477468807L;
	private Map<String, Object> session;
	private String id;
	private String pass;
	private String role;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private StudentService studentService;
	
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> sessionMap) {
		this.session = sessionMap;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public String getRole() {
		return role;
	}
	public void setRole(String role) {
		this.role = role;
	}
	public String execute() throws Exception{
		if("teacher".equals(role)){
			if(id.equals("") || pass.equals("")){
				this.addActionError("用户名或是密码不能为空！");
				return INPUT;
			}else{
				if(teacherService.allowLogin(id, pass)){
					Teacher teacherInfo = teacherService.getTeacherInfo(id);
					session.put("teacherInfo", teacherInfo);
					return "teacherSuccess";
				}else{
					this.addActionError("编号或是密码错误！");
					return INPUT;
				}
			}	
		}else{
			if(id.equals("") || pass.equals("")){
				this.addActionError("用户名或是密码不能为空！");
				return INPUT;
			}else{
				if(studentService.allowLoagin(id, pass)){
					Student studentInfo = studentService.getStudentInfo(id);
					session.put("studentInfo", studentInfo);
					return "studentSuccess";
				}else{
					this.addActionError("编号或是密码错误！");
					return INPUT;
				}
			}
		}
	}
}
